//
//  HCVisitor.swift
//  新浪微博
//
//  Created by fanhoucheng on 16/9/23.
//  Copyright © 2016年 fanhoucheng. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol HCVisitorDelegate: NSObjectProtocol {
    
    //代理方法
    func userWillLogin()
    
    @objc optional func userWillRegister()
}

class HCVisitor: UIView {
    
    weak var delegate: HCVisitorDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置背景颜色
        let colorValue = CGFloat(237 / 255.0)
        self.backgroundColor = UIColor(red: colorValue, green: colorValue, blue: colorValue, alpha: 1)
        
        setupUI()
        
    }
    
    func updateUI(describeLableName: String, imageName: String? = nil) {
        describeLable.text = describeLableName
        
        if imageName == nil {
            //如果为空,就是首页,执行动画
            animation()
        } else {
            circleView.image = UIImage(named: imageName!)
            iconView.isHidden = true
            bringSubview(toFront: circleView)
        }
    }
    
    func animation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = M_PI * 2
        animation.duration = 15
        animation.repeatCount = Float(INT_MAX)
        animation.isRemovedOnCompletion = false
        
        circleView.layer.add(animation, forKey: nil)
    }
    
    func setupUI() {
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(describeLable)
        addSubview(loginButton)
        addSubview(registerButton)
        
        circleView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        iconView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        describeLable.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(8)
            make.centerX.equalTo(circleView.snp.centerX)
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.top.equalTo(describeLable.snp.bottom).offset(8)
            make.left.equalTo(describeLable)
            make.width.equalTo(100)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.top.equalTo(describeLable.snp.bottom).offset(8)
            make.right.equalTo(describeLable)
            make.width.equalTo(100)
        }
        
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        //为注册和登录按钮添加target
        loginButton.addTarget(self, action: #selector(loginButtonDidClick), for: .touchUpInside)
        registerButton.addTarget(self, action: #selector(registerButtonDidClick), for: .touchUpInside)
    }
    
    func loginButtonDidClick() {
        //用户登录
        delegate?.userWillLogin()
    }
    
    func registerButtonDidClick() {
        //用户注册
        delegate?.userWillRegister?()
    }
    
    private lazy var circleView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    private lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    private lazy var describeLable: UILabel = {
        let label = UILabel()
        label.text = "我哦我我我我我我我我我哦我我我我我我我哦我我我哦我"
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = 224
        label.textAlignment = .center
        
        return label
    }()
    
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.sizeToFit()
        button.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        return button
    }()
    
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.sizeToFit()
        button.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        return button
    }()
    
    private lazy var backView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
